package com.ktgj.file.controller;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ktgj.customer.api.RemoteCustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ktgj.common.core.domain.R;
import com.ktgj.common.core.utils.file.FileUtils;
import com.ktgj.file.service.ISysFileService;
import com.ktgj.system.api.domain.SysFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文件请求处理
 *
 * @author ruoyi
 */
//@ControllerAdvice
@RestController
@RequestMapping("fileMapping")
public class SysFileController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    @Qualifier("MinioSysFileServiceImpl")
    private ISysFileService sysFileService;

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<List<SysFile>> upload(HttpServletRequest request, MultipartFile[] file) {
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadFile(file, request.getHeader("source"), request.getHeader("business"));
            List<SysFile> sysFileList = new ArrayList<>();
            for (String s : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(s));
                sysFile.setUrl(s);
                sysFileList.add(sysFile);
            }

            return R.ok(sysFileList);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 上传图片
     * */
    @PostMapping("uploadAvatar")
    public List<String> uploadAvatar(List<MultipartFile> file, String type) {
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传会员头像
     * */
    @PostMapping("uploadAvatarCustomer")
    public List<String> uploadAvatarCustomer(List<MultipartFile> file) {
        String type = "customer";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传员工头像
     * */
    @PostMapping("uploadAvatarEmployee")
    public List<String> uploadAvatarEmployee(List<MultipartFile> file) {
        String type = "employee";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传工单图片
     * */
    @PostMapping("uploadAvatarWorkOrder")
    public List<String> uploadAvatarWorkOrder(List<MultipartFile> file) {
        String type = "workOrder";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传机场图片
     * */
    @PostMapping("uploadAvatarAirport")
    public List<String> uploadAvatarAirport(List<MultipartFile> file) {
        String type = "airport";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传机型图片
     * */
    @PostMapping("uploadAvatarModel")
    public List<String> uploadAvatarModel(List<MultipartFile> file) {
        String type = "model";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传贵宾厅图片
     * */
    @PostMapping("uploadAvatarVIPHall")
    public List<String> uploadAvatarVIPHall(List<MultipartFile> file) {
        String type = "VIPHall";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传贵宾厅图片
     * */
    @PostMapping("uploadAvatarMall")
    public List<String> uploadAvatarMall(List<MultipartFile> file) {
        String type = "mall";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传贵宾厅套餐图片
     * */
    @PostMapping("uploadAvatarCombo")
    public List<String> uploadAvatarCombo(List<MultipartFile> file) {
        String type = "combo";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传保险理赔图片
     * */
    @PostMapping("uploadClaim")
    public List<String> uploadClaim(List<MultipartFile> file) {
        String type = "claim";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 航司LogoUrl
     * */
    @PostMapping("airlineLogo")
    public String airlineLogo() {
        String logoUrl = sysFileService.airlineLogo();
        return logoUrl;
    }

    /**
     * 上传文件到服务器
     * @param file 文件流
     * @return
     */
    @PostMapping("/document")
    public File document(MultipartFile file) {
        File document = sysFileService.document(file);
        return document;
    }

    /**
     * 文件上传请求(对账)
     */
    @PostMapping("fileUpload")
    public R<List<SysFile>> fileUpload(MultipartFile[] file) {
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadFile(file, "reconciliation", "upload");
            List<SysFile> sysFileList = new ArrayList<>();
            for (int i = 0 ; i < stringList.size() ; i++) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(stringList.get(i)));
                sysFile.setUrl(stringList.get(i));
                sysFile.setOdlName(FileUtils.getName(file[i].getOriginalFilename()));
                sysFileList.add(sysFile);
            }
            return R.ok(sysFileList);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 合同附件上传
     */
    @PostMapping("contractUpload")
    public R<List<SysFile>> contractUpload(MultipartFile[] file) {
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadFile(file, "contract", "upload");
            List<SysFile> sysFileList = new ArrayList<>();
            for (int i = 0 ; i < stringList.size() ; i++) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(stringList.get(i)));
                sysFile.setUrl(stringList.get(i));
                sysFile.setOdlName(FileUtils.getName(file[i].getOriginalFilename()));
                sysFileList.add(sysFile);
            }
            return R.ok(sysFileList);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 上传旅游图片
     * */
    @PostMapping("uploadTravel")
    public List<String> uploadTravel(List<MultipartFile> file) {
        String type = "travel";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }

    /**
     * 上传酒店图片
     * */
    @PostMapping("uploadHotel")
    public List<String> uploadHotel(List<MultipartFile> file) {
        String type = "hotel";
        List<String> urlList = new ArrayList<>();
        try {
            // 上传并返回访问地址
            List<String> stringList = sysFileService.uploadAvatar(file, type);
            for (String url : stringList) {
                SysFile sysFile = new SysFile();
                sysFile.setName(FileUtils.getName(url));
                sysFile.setUrl(url);
                urlList.add(R.ok(sysFile).getData().getUrl());
            }

            return urlList;
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return null;
        }
    }
}